// pages/my/myOrder/myOrder.js
const App = getApp();
const utils = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 快递选择
    expressList: ['默认快递','顺丰到付'],
    expressIndex: 0,
    sendName:'',
    //是否有地址
    isAddress:wx.getStorageSync('addressAddress'),
    name:wx.getStorageSync('addressName'),
    phone:wx.getStorageSync('addressPhone'),
    addressId:wx.getStorageSync('addressId'),
    sendType:'',
    usercomment:'',
    cupName:'暂无可用',
    sfcupName:'暂无可用',
    orderName:'包邮',
    isCup:true,
    stopSend:false,
    type:0
  },
   // 选择发货函数
   changeExpress(e){
    this.setData({ expressIndex: e.detail.value});
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      kutoysids:options.kutoysids
    })
    this.getDataList(options.kutoysids)
    this.issfsend(options.kutoysids)
    this.stopKD()

    let date = new Date();
    let month = date.getMonth() + 1;
    let day = date.getDate();
    if (month == 1 && 1<= day && day <= 3){
        this.setData({
          stopSend:true
        })
    } else {
      this.setData({
        stopSend:false
      })
    }
  },
  //添加地址
  addAddress:function () {
    wx.navigateTo({
      url: '/pages/my/addressAdmin/addressAdmin?type=0',
    })
  },
  //快递停发通知
  stopKD:function () {
    utils.requestUrl({
      url: "/niu/apiv4/niu_fahuo_message.php",
      params: {
        markid: wx.getStorageSync('markId'),
      }
    }).then((res) => {
      var data = res.data;
      console.log(data);
      var stopSendTitle = data.title
      var stopSendData = data.data
      this.setData({
        stopSendTitle:stopSendTitle,
        stopSendData:stopSendData,
      })
    })
  },
  //判断是否顺丰包邮
  issfsend:function (kutoysids) {
    utils.requestUrl({
      url: "/niu/apiv3/fahuo_issfsend.php",
      params: {
        markid: wx.getStorageSync('markId'),
        'goodsids':kutoysids
      }
    }).then((res) => {
      var data = res.data;
      console.log(data);
      var cupName,orderName,sendType;
      if(data.data.issfpack === '3'){
        cupName = '该订单包邮';
        orderName = '顺丰包邮';
        sendType = 1
      }else if(data.data.issfpack === '1'){
        cupName = '该订单包邮';
        orderName = '包邮';
        sendType = 1
      }else{
        sendType = 0
        this.isCupList();
      }
      this.setData({
        // cupNum:cupNum,
        // isCup:isCup,
        sendType:sendType,
        cupName:cupName,
        orderName:orderName
      })
    })
  },
  //判断是否有包邮劵
  isCupList:function(){
    utils.requestUrl({
      url: "/niu/apiv4/fahuo_dosend_my_coupon.php",
      params: {
        markid: wx.getStorageSync('markId'),
        type:0
      }
    }).then((res) => {
      var data = res.data;
      console.log(data);
      //包邮劵
      let cupNum = Number(data.info[0].num);
      console.log(cupNum)
      var isCup;
      if (cupNum > 0){
        isCup = true
      }else{
        isCup = false
      }
      this.setData({
        cupNum:cupNum,
        sfcupNum:data.sfnum,
        isCup:isCup
      })
    })
  },
  //是否使用优惠券
  isCheck:function(){
    var isCup = this.data.isCup;
    var isCup2 = !isCup
    this.setData({
      isCup:isCup2,
      issfCup:false,
      type:0
    })
  },
  //是否使用顺丰包邮优惠券
  issfCheck:function(){
    var issfCup = this.data.issfCup;
    var type = this.data.type
    var issfCup2 = !issfCup;
    if(issfCup2){
      type = 2
    }else{
      type = 0    
    }
    this.setData({
      isCup:false,
      issfCup:issfCup2,
      type:type
    })
  },
  //使用包邮劵提交订单
  orderBtn:function () {
    var that = this;
    var goodsids = that.data.kutoysids;
    var adid =  that.data.addressId;
    var usercomment = that.data.usercomment;
    var kdtype = that.data.expressIndex;
    var type = that.data.type;
    if (type == 2){
      kdtype = 3
    }
    utils.requestUrl({
      url: "/niu/apiv4/fahuo_baoyou.php",
      params: {
        markid: wx.getStorageSync('markId'),
        goodsids: goodsids,
        adid: adid,
        usercomment: usercomment,
        kdtype:kdtype,
        type:type
      }
    }).then((res) => {
      var data = res.data;
      console.log(data);
      if (data.resultcode == 0) {
        utils.toast(data.msg);
        setTimeout(function(){
          wx.navigateBack({
            delta: 2
          })
        },1000)
      }else{
        utils.toast(data.msg)
      }
    })
  },

  //获取发货列表
  getDataList:function (kutoysids) {
    utils.requestUrl({
      url: "/niu/apiv3/fahuo_getkutoylist.php",
      params: {
        markid: wx.getStorageSync('markId'),
        kutoysids:kutoysids
      }
    }).then((res) => {
      var data = res.data;
      console.log(data);
      var expressList;
      if (data.totalcost <=50){
        expressList = ['默认快递']
      }else{
        expressList = ['默认快递','顺丰到付']
      }
      // if (data.sendtype === 1){
      //   var cupName = '该订单包邮'
      // }else{
      //   this.isCupList();
      // }
      
      this.setData({
        list:data.data,
        expressList:expressList,
        sendName:data.sendtypename,
        // cupName:cupName,
        cupNum:0,
        isCup:false
      })
    })
  },
  //判断运费
  checkYunPrice:function () {
    utils.requestUrl({
      url: "/niu/apiv3/fahuo_checkyun.php",
      params: {
        markid: wx.getStorageSync('markId'),
        local1: wx.getStorageSync('addressLocal1'),
        local2: wx.getStorageSync('addressLocal2'),
        local3: wx.getStorageSync('addressLocal3'),
      }
    }).then((res) => {
      var data = res.data;
      console.log(data);
      this.setData({
        yunPrice:data.price
      })
    })
  },
  //
  //发货(包邮)
  sendOrder:function () {
    var that = this;
    var goodsids = that.data.kutoysids;
    var adid =  that.data.addressId;
    var usercomment = that.data.usercomment;
    var kdtype = that.data.expressIndex
    utils.requestUrl({
      url: "/niu/apiv3/fahuo_dosend.php",
      params: {
        markid: wx.getStorageSync('markId'),
        goodsids: goodsids,
        adid: adid,
        usercomment: usercomment,
        kdtype:kdtype
      }
    }).then((res) => {
      var data = res.data;
      console.log(data);
      if (data.resultcode == 0) {
        utils.toast(data.msg);
        setTimeout(function(){
          wx.navigateBack({
            delta: 2
          })
        },1000)
      }else{
        utils.toast(data.msg)
      }
    })
  },
  //发货 （需要支付运费）
  advanceOrder:function () {
    var that = this;
    var goodsids = that.data.kutoysids;
    var adid =  that.data.addressId;
    var usercomment = that.data.usercomment;
    console.log(adid)
    if(adid == ''){
      utils.toast('地址不能为空')
      return
    }

    console.log(adid)
    utils.requestUrl({
      url: "/niu/apiv3/fahuo_tmp_dosend.php",
      params: {
        markid: wx.getStorageSync('markId'),
        goodsids: goodsids,
        adid: adid,
        usercomment: usercomment,
        kdtype:0
      }
    }).then((res) => {
      var data = res.data;
      console.log(data);
      if (data.resultcode == 0) {
        that.payYunPrice(data.paycode)
      }else{
        utils.toast(data.msg)
      }
    })
  },
  //运费支付
  payYunPrice:function (paycode) {
    utils.requestUrl({
      url: "/niu/wxpay/wxprepay_wxsapp.php?paytype=yunfei",
      params: {
        markid: wx.getStorageSync('markId'),
        payforid: paycode,
        openid: wx.getStorageSync('openId')
      }
    }).then((res) => {
      var data = res.data;
      console.log(data);
      wx.requestPayment({
        timeStamp: data.timeStamp,
        nonceStr: data.nonceStr,
        package: data.package,
        signType: 'MD5',
        paySign: data.paySign,
        success (res) {
          console.log(res)
          if(res.errMsg == 'requestPayment:ok'){
            utils.toast('支付成功')
            setTimeout(function(){
              wx.navigateBack({
                delta: 2
              })
            },1000)
          }
        },
        fail (res) {
          console.log(res)
          utils.toast('支付失败,请重新支付')
        }
      })
    })
  },
  //用户备注
  usercomment:function (e) {
    this.setData({
      usercomment: e.detail.value
    })
  },
  goToDetail:function (e){
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/my/myIndexDetail/myIndexDetail?id='+id,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      name:wx.getStorageSync('addressName'),
      phone:wx.getStorageSync('addressPhone'),
      isAddress:wx.getStorageSync('addressAddress'),
      addressId:wx.getStorageSync('addressId'),
    })

    var adid =  this.data.addressId;
    console.log(adid)
    // 判断是否包邮 不包邮需要运费
    if (this.data.sendType == 0 && adid != ''){
      this.checkYunPrice();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})